package com.example.shiwu;

import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.shiwu.db.model.BoxElectrodeRo;
import com.example.shiwu.db.model.CvsBean;
import com.example.shiwu.db.model.Sends;
import com.example.shiwu.manager.DynamicLineChartManager;
import com.example.shiwu.service.BoxElectrodeRoService;
import com.example.shiwu.service.SendsService;
import com.example.shiwu.util.ContextVariable;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class ElectrodeRoDetailsActivity extends AppCompatActivity {
    public TextView title_text, title_setting;
    public ImageView title_back;
    private BoxElectrodeRoService boxElectrodeRoService;
    private static final int MSG_RECEIVE_DATA = 1;
    private View contentView;
    private MyAdapter mMyAdapter;
    private RecyclerView mRecyclerView;
    public static Context myContext;
    private List<BoxElectrodeRo> boxElectrodeRo;
    private static final String TAG = MainActivity.class.getSimpleName();
    List<String> mNewsList = new ArrayList<>();
    private Handler mHandler = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case MSG_RECEIVE_DATA:
                    mRecyclerView = findViewById(R.id.recyclerviewlist);
                    mMyAdapter = new MyAdapter();
                    mRecyclerView.setAdapter(mMyAdapter);
                    LinearLayoutManager layoutManager = new LinearLayoutManager(myContext);
                    mRecyclerView.setLayoutManager(layoutManager);
                    break;
                default:
                    break;
            }
        }

        ;
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        myContext = this;
        setContentView(R.layout.electrode_ro_details);
        title_back = findViewById(R.id.title_back);
        title_text = findViewById(R.id.title_text);
        title_text.setText("电极电阻详情");
        title_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ElectrodeRoDetailsActivity.this.finish();
            }
        });
        Intent intent = getIntent();
        String itemId = intent.getStringExtra("itemId");
        boxElectrodeRoService = new BoxElectrodeRoService();
        List<BoxElectrodeRo> boxElectrodeRo = boxElectrodeRoService.getBoxElectrodeRo(new BoxElectrodeRo(itemId));
        for (int i = 0; i < boxElectrodeRo.size(); i++) {
            String boxId = boxElectrodeRo.get(i).getBoxId();
            String boxIdVal = boxId;
            mNewsList.add(boxIdVal+"_1 "+boxIdVal+"_2 = "+boxElectrodeRo.get(i).getOneRo());
            mNewsList.add(boxIdVal+"_2 "+boxIdVal+"_3 = "+boxElectrodeRo.get(i).getTwoRo());
            mNewsList.add(boxIdVal+"_3 "+boxIdVal+"_4 = "+boxElectrodeRo.get(i).getThreeRo());
            mNewsList.add(boxIdVal+"_4 "+boxIdVal+"_5 = "+boxElectrodeRo.get(i).getFourRo());
            mNewsList.add(boxIdVal+"_5 "+boxIdVal+"_6 = "+boxElectrodeRo.get(i).getFiveRo());
            mNewsList.add(boxIdVal+"_6 "+boxIdVal+"_7 = "+boxElectrodeRo.get(i).getSixRo());
            mNewsList.add(boxIdVal+"_7 "+boxIdVal+"_8 = "+boxElectrodeRo.get(i).getSevenRo());
            mNewsList.add(boxIdVal+"_8 "+boxIdVal+"_9 = "+boxElectrodeRo.get(i).getEightRo());
            mNewsList.add(boxIdVal+"_9 "+boxIdVal+"_10 = "+boxElectrodeRo.get(i).getNineRo());
            mNewsList.add(boxIdVal+"_10 "+boxIdVal+"_11 = "+boxElectrodeRo.get(i).getTenRo());
            mNewsList.add(boxIdVal+"_11 "+boxIdVal+"_12 = "+boxElectrodeRo.get(i).getElevenRo());
        }
        Message msg1 = new Message();
        msg1.what = MSG_RECEIVE_DATA;
        mHandler.sendMessage(msg1);
    }
    class MyAdapter extends RecyclerView.Adapter<MyViewHoder> {

        @NonNull
        @Override
        public MyViewHoder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = View.inflate(ElectrodeRoDetailsActivity.this, R.layout.dianji_list_item, null);
            MyViewHoder myViewHoder = new MyViewHoder(view);
            return myViewHoder;
        }

        @Override
        public void onBindViewHolder(@NonNull MyViewHoder holder, int position) {
//            holder.mTitleTv.setText((position+1)+" "+mNewsList.get(position));
            String[] slit = mNewsList.get(position).split(":");
            holder.mTitleId.setText(slit[0]);
            holder.mTitleTv.setText(slit[1]);
            holder.mTitleVal.setText(slit[2]);
        }

        @Override
        public int getItemCount() {
            return mNewsList.size();
        }
    }

    class MyViewHoder extends RecyclerView.ViewHolder {
        TextView mTitleTv,mTitleId,mTitleVal;

        public MyViewHoder(@NonNull View itemView) {
            super(itemView);
            mTitleId = itemView.findViewById(R.id.dianjiid);
            mTitleTv = itemView.findViewById(R.id.dianji);
            mTitleVal = itemView.findViewById(R.id.dianjival);
        }

    }
}